package com.fate.system.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fate.framework.dto.IdParamDTO;
import com.fate.framework.utils.SystemUtil;
import com.fate.system.dto.org.*;
import com.fate.system.dto.userorg.UserOrgAddDTO;
import com.fate.system.entity.Org;
import com.fate.system.service.IOrgService;
import com.fate.system.service.IUserOrgService;
import com.fate.system.vo.org.OrgDetailVO;
import com.fate.system.vo.org.OrgTreeVO;
import com.fate.system.vo.user.UserListVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "组织机构操作 API")
@RestController
@RequestMapping("/api/v1.0/org")
public class OrgController {

  @Resource private IOrgService orgService;
  @Resource private IUserOrgService userOrgService;

  @SaCheckRole("super_admin")
  @Operation(summary = "新增")
  @PostMapping("/add")
  public void add(@RequestBody @Valid OrgAddDTO addDTO) {
    orgService.addOrg(addDTO);
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "删除")
  @PostMapping("/remove")
  public void remove(@RequestBody @Valid IdParamDTO idParamDTO) {
    orgService.removeOrg(idParamDTO.getId());
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "更新")
  @PostMapping("/update")
  public void update(@RequestBody @Valid OrgUpdateDTO updateDTO) {
    orgService.updateOrg(updateDTO);
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "详情")
  @PostMapping("/detail")
  public OrgDetailVO detail(@RequestBody @Valid IdParamDTO idParamDTO) {
    Org org = orgService.getById(idParamDTO.getId());
    return SystemUtil.buildResource(org, OrgDetailVO.class);
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "组织机构树形结构")
  @PostMapping("/treeList")
  public List<OrgTreeVO> treeList() {
    return orgService.treeList();
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "组织机构用户列表")
  @PostMapping("/getOrgUserList")
  public Page<UserListVO> getOrgUserList(@RequestBody @Valid OrgUserPageDTO orgUserPageDTO) {
    return orgService.getOrgUserList(orgUserPageDTO);
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "设置用户所属组织")
  @PostMapping("/setUser2Org")
  public void setUser2Org(@RequestBody @Valid UserOrgAddDTO userOrgAddDTO) {
    userOrgService.setUser2Org(userOrgAddDTO.getUserId(), userOrgAddDTO.getOrgId());
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "移除用户所属组织")
  @PostMapping("/removeUserFromOrg")
  public void removeUserFromOrg(@RequestBody @Valid DelUserOrgDTO delUserOrgDTO) {
    userOrgService.removeUserFromOrg(delUserOrgDTO.getUserId(), delUserOrgDTO.getOrgId());
  }
}
